<template>
  <div>
    <el-form ref="formRef" :model="form" label-width="120px">
      <UserSearchComponent @setUserInfo="setUserInfo" />
      <UserInfo :userInfo="userInfo" />
      <el-form-item label="申请原因" prop="remark">
        <div style="display: flex; flex-direction: column">
          <el-input
            v-model="form.remark"
            type="textarea"
            :maxlength="100"
            :show-word-limit="true"
            :input-style="{ height: '100px', width: '300px' }"
          />
        </div>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          class="primary"
          @click="submitForm(formRef)"
          :disabled="!form.orderStudent"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="tsx" setup>
import { reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import UserSearchComponent from '@/components/UserSearchComponent/index.vue';
import UserInfo from '@/components/UserInfo/index.vue';
const emit = defineEmits(['submit']);
import { get } from 'lodash';
const formRef = ref<FormInstance>();
// 用户信息
const userInfo: { [key: string]: any } = reactive({
  用户信息: '',
  用户成绩: '分 位',
  历史变更次数: '次',
});
// 表单数据
const form = reactive({
  remark: '',
  orderStudent: '',
});

// 表单提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      const params = {
        // 用户信息
        nick: userInfo['用户信息'],
        // 用户修改前成绩
        aftarScore: userInfo['用户成绩'],
        // 历史变更次数
        afterGkb_score_num: userInfo['历史变更次数'],
      };
      emit('submit', { ...form, ...params });
    }
  });
};
const setUserInfo = (val: any) => {
  const params = {
    用户信息: get(val, 'nick', ''), // 默认为空字符串
    用户成绩: get(val, 'score', '') + '分' + get(val, 'section', '') + '位',
    历史变更次数: get(val, 'gkb_score_num', 0) + '次',
  };

  Object.assign(userInfo, params);
  form.orderStudent = get(val, 'phone', '');
};
</script>

<style lang="scss" scoped>
.primary {
  width: 170px;
  color: #fff;
  background-color: #165dff;
  border-color: #165dff;
  height: 40px;
}

.primary:hover {
  opacity: 0.8;
}
</style>
